package com.whfc.iot.session.impl;

import com.whfc.iot.session.SessionManager;
import com.whfc.iot.session.SessionSynchronizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Description: session同步
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/18 14:42
 */
public class SessionSynchronizerImpl implements SessionSynchronizer {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private String borkerName;

    private SessionManager sessionManager;

    public SessionSynchronizerImpl() {
    }

    public SessionSynchronizerImpl(String borkerName, SessionManager sessionManager) {
        this.borkerName = borkerName;
        this.sessionManager = sessionManager;
    }

    @Override
    public void run() {
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        executor.scheduleAtFixedRate(() -> {
            Map<String, String> devices = sessionManager.getAllOnlineDevice();
            logger.info("在线设备数量:{},设备ID:{}", devices.size(), devices.keySet());
        }, 10, 30, TimeUnit.SECONDS);
    }
}
